package com.hsjk.szwj.module.train.controller.app;

import cn.hutool.core.collection.CollUtil;
import com.hsjk.szwj.framework.common.pojo.CommonResult;
import com.hsjk.szwj.framework.common.pojo.PageResult;
import com.hsjk.szwj.framework.security.core.annotations.PreAuthenticated;
import com.hsjk.szwj.module.train.controller.app.vo.product.AllNormalSpuPageReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.product.CategoryListRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.product.NormalSpuPageByCategoryReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.product.SkuDetailRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.product.SpuDetailRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.product.SpuListRespVO;
import com.hsjk.szwj.module.train.convert.ProductConvert;
import com.hsjk.szwj.module.train.dal.dataobject.product.ProductCategoryDO;
import com.hsjk.szwj.module.train.dal.dataobject.product.ProductSkuBindDO;
import com.hsjk.szwj.module.train.dal.dataobject.product.ProductSkuDO;
import com.hsjk.szwj.module.train.dal.dataobject.product.ProductSpuDO;
import com.hsjk.szwj.module.train.service.IProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ec50n9
 */
@Tag(name = "应用端 - 商城相关接口")
@RestController
@RequestMapping("/train/product")
@Validated
@Slf4j
public class AppProductController {

    @Resource
    private IProductService productService;

    @GetMapping("/get-sku-by-id")
    @Operation(summary = "获取课程标签列表")
    @PreAuthenticated
    public CommonResult<SkuDetailRespVO> getSkuById(Long skuId) {
        ProductSkuDO productSku = productService.getCourseBindProductSkuById(skuId);
        SkuDetailRespVO skuDetailRespVO = ProductConvert.INSTANCE.convertSku(productSku);
        List<ProductSkuBindDO> skuBindDOList = productService.getCourseBindProductSkuBindListBySkuId(
                skuId);
        skuDetailRespVO.setSkuBindList(ProductConvert.INSTANCE.convertSkuBind(skuBindDOList));
        return CommonResult.success(skuDetailRespVO);
    }

    @PostMapping("/get-all-normal-spu-page")
    @Operation(summary = "获取所有普通的商品分页列表")
    @PreAuthenticated
    public CommonResult<PageResult<SpuListRespVO>> getAllNormalSpuPage(
            @RequestBody AllNormalSpuPageReqVO reqVO
    ) {
        return CommonResult.success(productService.getNormalProductSpuPageByCarType(reqVO.getCarType(), reqVO));
    }

    @PostMapping("/get-normal-spu-page-by-category-id")
    @Operation(summary = "根据分类id获取普通商品分页列表")
    @PreAuthenticated
    public CommonResult<PageResult<SpuListRespVO>> getNormalSpuPageByCategoryId(
            @RequestBody NormalSpuPageByCategoryReqVO reqVO
    ) {
        PageResult<ProductSpuDO> spuPageByCategoryId = productService.getNormalProductSpuPageByCategoryId(
                reqVO.getCategoryId(), reqVO);
        List<SpuListRespVO> listRespVOS = ProductConvert.INSTANCE.convertSpu(
                spuPageByCategoryId.getList());
        return CommonResult.success(new PageResult<>(listRespVOS, spuPageByCategoryId.getTotal()));
    }

    @GetMapping("/get-normal-spu-detail-by-id")
    @Operation(summary = "根据商品id获取普通商品详情")
    @PreAuthenticated
    public CommonResult<SpuDetailRespVO> getNormalSpuDetailById(Long spuId) {
        ProductSpuDO spuDO = productService.getNormalProductSpuById(spuId);
        List<ProductSkuDO> skuDOList = productService.getProductSkuListBySpuId(spuId);
        SpuDetailRespVO spuDetailRespVO = ProductConvert.INSTANCE.convertSpu(spuDO);
        spuDetailRespVO.setSkuList(ProductConvert.INSTANCE.convertSku(skuDOList));
        List<String> tags = productService.getTagsBySpuId(spuId);
        if (CollUtil.isNotEmpty(tags)) {
            spuDetailRespVO.setTags(tags);
        }
        return CommonResult.success(spuDetailRespVO);
    }

    @GetMapping("/get-all-category-list")
    @Operation(summary = "获取所有分类列表")
    @PreAuthenticated
    public CommonResult<List<CategoryListRespVO>> getAllCategoryList() {
        List<ProductCategoryDO> categoryList = productService.getProductCategoryList();
        return CommonResult.success(ProductConvert.INSTANCE.convertCategory(categoryList));
    }
}
